﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Caching;
using System.IO;


namespace NetOpen_System.Component
{
    public sealed class WebsiteSkipConfiguration
    {
        
        
		#region 构造函数
		/// <summary>
		/// 缓存键名称
		/// </summary>
        public static readonly string CacheKey = "WebsiteSkipConfig";
		private XmlDocument XmlDoc = null;
        private WebsiteSkipConfiguration(XmlDocument doc)
		{
			XmlDoc = doc;
			LoadValuesFromConfigurationXml();
		}

		#endregion



        #region 获取配置信息
        /// <summary>
        /// 获取配置节点
        /// </summary>
        /// <param name="nodePath">XPath 表达式</param>
        public XmlNode GetConfigSection(string nodePath)
        {
            return XmlDoc.SelectSingleNode(nodePath);
        }

        /// <summary>
        /// 获取默认的配置实例
        /// </summary>
        public static WebsiteSkipConfiguration GetConfig()
        {
            WebsiteSkipConfiguration config = GlobesCache.Get(CacheKey) as WebsiteSkipConfiguration;
            if (config == null)
            {
                string path = null;
                HttpContext context = HttpContext.Current;

                if (context != null)
                    path = context.Server.MapPath(string.Format("~/{0}", cntConfigFileName));
                else
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cntConfigFileName);

                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                config = new WebsiteSkipConfiguration(doc);
                GlobesCache.Max(CacheKey, config, new CacheDependency(path));
            }
            return config;

        }



        internal void LoadValuesFromConfigurationXml()
        {
            XmlNode node = null;

            #region PublishUrl

            node = GetConfigSection("NetOpen_System/WebsiteSkip/SkipedUrl");
            m_SkipedUrls = new UrlMatchEngine(node);

            #endregion


            #region AllowedIP

            node = GetConfigSection("NetOpen_System/WebsiteSkip/SkipedIP");
            m_IpChecks = new IPCheck(node);

            #endregion

            #region ExcludeUserAgent

            node = GetConfigSection("NetOpen_System/WebsiteSkip/ExcludeUserAgent");
            m_ExcludeUserAgents = new ExcludeUserAgentMatchEngine(node);

            #endregion


        }

        

        #endregion

        #region 配置字段


        private UrlMatchEngine m_SkipedUrls;
        private IPCheck m_IpChecks;
        private ExcludeUserAgentMatchEngine m_ExcludeUserAgents;


        #endregion


        #region 配置相关

       
        /// <summary>
        /// 预定义的Url列表
        /// </summary>
        public UrlMatchEngine SkipedUrls
        {
            get { return m_SkipedUrls; }
        }

        /// <summary>
        /// 预定义的IP检查类
        /// </summary>
        public IPCheck IpChecks
        {
            get { return m_IpChecks; }
        }

        /// <summary>
        /// 用户代理信息关键字排除列表
        /// </summary>
        public ExcludeUserAgentMatchEngine ExcludeUserAgents
        {
            get { return m_ExcludeUserAgents; }
        }

        #endregion

        private const string cntConfigFileName = "NetOpen_SystemWebsiteSkip.cfg.xml";
    }
}
